/*
 * @lc app=leetcode.cn id=5 lang=cpp
 *
 * [5] 最长回文子串
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    string longestPalindrome(string s)
    {
        const int N = s.size();
        vector<vector<bool>> dp(
            s.size(), vector<bool>(s.size(), false));

        int current_max_len = 1;
        int ans_start_inx = 0;
        for (int i = 0; i < N - 1; i++)
        {
            dp[i][i] = true;
            if ((s[i] == s[i + 1]))
            {
                dp[i][i + 1] = true;
                ans_start_inx = i;
                current_max_len = 2;
            }
        }
        dp[N - 1][N - 1] = true;

        for (int L = 3; L <= N; L++) // 枚举字符串长度，长度为1和2的都已经处理了
        {
            for (int i = 0; i <= N - L; i++)
            {
                int j = i + L - 1;
                dp[i][j] = dp[i + 1][j - 1] && (s[i] == s[j]);
                if(dp[i][j])
                {
                    ans_start_inx = i;
                    current_max_len = L;
                }
            }
        }

        return s.substr(ans_start_inx, current_max_len);
    }
};
// @lc code=end
